/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.alarm;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarmGroup;
import com.thinkgem.jeesite.modules.source.service.alarm.EleSourceAlarmGroupService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 警告资源管理Controller
 * @author ZC
 * @version 2017-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/source/alarm/eleSourceAlarmGroup")
public class EleSourceAlarmGroupController extends BaseController {

	@Autowired
	private EleSourceAlarmGroupService eleSourceAlarmGroupService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public EleSourceAlarmGroup get(@RequestParam(required=false) String id) {
		EleSourceAlarmGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceAlarmGroupService.get(id);
		}
		if (entity == null){
			entity = new EleSourceAlarmGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceAlarmGroup eleSourceAlarmGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());		
		}		
		eleSourceAlarmGroup.setUsers(list);
		Page<EleSourceAlarmGroup> page = eleSourceAlarmGroupService.findPage(new Page<EleSourceAlarmGroup>(request, response), eleSourceAlarmGroup); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		model.addAttribute("language", locale.getLanguage());
		if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
			model.addAttribute("langCode", locale.getLanguage()+"-cn");
		}else{
			model.addAttribute("langCode", locale.getLanguage());
		}
		return "modules/source/alarm/eleSourceAlarmGroupList";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:view")
	@RequestMapping(value = "form")
	public String form(EleSourceAlarmGroup eleSourceAlarmGroup, Model model) {
		model.addAttribute("eleSourceAlarmGroup", eleSourceAlarmGroup);
		return "modules/source/alarm/eleSourceAlarmGroupForm";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceAlarmGroup eleSourceAlarmGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleSourceAlarmGroup)){
			return form(eleSourceAlarmGroup, model);
		}
		eleSourceAlarmGroupService.save(eleSourceAlarmGroup);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存警告资源成功";
		if(Global.US.equals(locale)){
			msg = "Alarm resource saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceAlarmGroup eleSourceAlarmGroup, RedirectAttributes redirectAttributes) {
		eleSourceAlarmGroupService.delete(eleSourceAlarmGroup);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除警告资源成功";
		if(Global.US.equals(locale)){
			msg = "Alarm resource deleted";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}

	@RequiresPermissions("source:alarm:eleSourceAlarmGroup:check")
	@RequestMapping(value = "check")
	public String check(EleSourceAlarmGroup eleSourceAlarmGroup, RedirectAttributes redirectAttributes) {
		eleSourceAlarmGroupService.check(eleSourceAlarmGroup);
		addMessage(redirectAttributes, "审核背景资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/alarm/eleSourceAlarmGroup/?repage";
	}
}